#python -u "d:\300manga\300MangaDownloader.py"

import os
import webbrowser

PicEigenvalues = "http://ww1.sinaimg.cn/"
PicEndEigenvalues = "</span>"
#webbrowser.register('chrome', None, webbrowser.BackgroundBrowser("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"))

MangaName = "伊甸的少女"
DownloadDir = "D:\\300manga\\"+MangaName+"\\"
SearchUrl = "https://bbs.yamibo.com/search.php?mod=forum&searchid=8193&orderby=lastpost&ascdesc=desc&searchsubmit=yes&kw=%D2%C1%B5%E9%B5%C4%C9%D9%C5%AE"
DownloadCommand = "E:\\Motrix\\resources\\engine\\aria2c.exe -d "
os.system(DownloadCommand+DownloadDir+" "+SearchUrl.replace("&","^&"))

def getPicUrl(DirName,* lines):
    count = 1
    for line in lines:
        if PicEigenvalues in line:
            str1 = line[line.find(PicEigenvalues):]
            PicUrl = str1[:str1.find(PicEndEigenvalues)]
            os.system(DownloadCommand+DownloadDir+DirName+" -o "+str(count).zfill(2)+".jpg "+PicUrl)
            count += 1

f1 = open(DownloadDir+"search.php","r",encoding="gbk")
lines = f1.readlines()
f1.close()
DirNames = []
OldNames = []
NewNames = []
count = 1
for line in lines:
    if "highlight" in line:
        str1 = line[line.find(">")+1:line.rfind("<")]
        DirName = str1.replace("<strong><font color=\"#ff0000\">","").replace("</font></strong>","").replace("&amp;","&")
        OldName = DownloadDir+str(count).zfill(2)+"\\"
        #os.mkdir(OldName)
        OldNames.append(OldName)
        NewName = DownloadDir+DirName+"\\"
        NewNames.append(NewName)
        print(OldName+"->"+NewName)
        count += 1
        DirNames.append(DirName)
        '''os.system("echo "+DirName+".php | clip")
        str3 = line[line.find("\"")+1:]
        PageUrl = "https://bbs.yamibo.com/"+str3[:str3.find("&amp;highlight")].replace("&amp;","&")
        webbrowser.open_new(PageUrl)
        print("done? y/e")
        Check = input()
        if Check == "y":
            continue
        if Check == "e":
            exit()'''
print()

count = 1
for DirName in DirNames:
    f3 = open(DownloadDir+DirName+".php","r",encoding="gb18030",errors="ignore")
    lines = f3.readlines()
    f3.close()
    index = str(count).zfill(2)+"\\"
    getPicUrl(index,* lines)
    count += 1
    continue

count = 0
while count < len(OldNames):
    print(OldNames[count]+"->"+NewNames[count])
    os.rename(OldNames[count],NewNames[count])
    count += 1